<?php

include "../inc/db_connect.php";
include "../inc/functions.php";
session_start();

if (empty($_SESSION['bmb_prihlas_id'])){
    echo 1;
    exit;
}

$sql = "SELECT * FROM prihlasenie WHERE id = '" . $_SESSION['bmb_prihlas_id'] . "'";
$result = MySQL_Query($sql);
$prihlasenie = MySQL_Fetch_Array($result);

if ($prihlasenie['datcas_do'] < date("Y-m-d H:i:s")) {
    echo 1;
    exit;
}

if ($prihlasenie['zedko']==1){
    //echo 1;
    //exit;
}

$sql = "SELECT * FROM user WHERE id = '" . $prihlasenie['uid'] . "'";
$result = MySQL_Query($sql);
$user = MySQL_Fetch_Array($result);

$sql = "SELECT * FROM oddelenie WHERE id = '" . $_REQUEST['oddelenie'] . "'";
$result = MySQL_Query($sql);
$oddelenie = MySQL_Fetch_Array($result);

$myFile = '../logs/vypocet_nadcasov.txt'; // 
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh,  date("d/m/Y H:i:s") . ' - ');
fwrite($fh,  $user['meno'] . ' ' . $user['priezvisko'] . ' (' .$user['mail']. ') - ');
fwrite($fh,  'tyzden: ' . date('d/m/Y', $_REQUEST['date']) . ' - ');
fwrite($fh,  $oddelenie['nazov']);
fwrite($fh,"\r\n");

$VYSTUP = '#';
$start = $zt = $kt = $_REQUEST['date'];

$c_i = "CREATE TABLE IF NOT EXISTS `vystup` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `zam` int(11) NOT NULL,
  `date` varchar(16) COLLATE utf8_slovak_ci NOT NULL,
  `value` double NOT NULL,
  `full` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_slovak_ci AUTO_INCREMENT=1 ;";
$c_q = MySQL_Query($c_i);

$t_i = "TRUNCATE TABLE `vystup`";
$t_q = MySQL_Query($t_i);

$VYSTUP = array();
while (date("n", $start) == date("n", $kt)) {
    $kt = $zt + (6 * 24 * 60 * 60);

    if ($kt > 1314489600) {


        $VYSTUP[0] = $zt;
        
        $zam_s = "SELECT * FROM z WHERE patri_k = '" . $_REQUEST['oddelenie'] . "'";
        $zam_q = MySQL_Query($zam_s);
        while ($zam = MySQL_Fetch_Array($zam_q)) {
            $VYSTUP[1] = $zam['id'];

            $prichod = explode('-', $zam['datum_prichodu']);
            $dPrichodu = date("U", mktime(0, 0, 0, $prichod[1], $prichod[2], $prichod[0]));
            if ($zam['datum_odchodu'] != '0000-00-00') {
                $odchod = explode('-', $zam['datum_odchodu']);
                $dOdchodu = date("U", mktime(0, 0, 0, $odchod[1], $odchod[2], $odchod[0]));
            } else {
                $dOdchodu = date("U", mktime(0, 0, 0, 12, 31, 2030)); // vymysleny datum ;-)
            }

            $full = false;
            if (($dPrichodu <= $zt) && ($dOdchodu >= $kt)) {
                $full = true;
                $VYSTUP[2] = 1;
            } else {
                $full = false;
                $VYSTUP[2] = 0;
                if (($dPrichodu > $kt) || ($dOdchodu < $zt)) {
                    $malBytVpraci = 0;
                } else if (($dPrichodu > $zt) && ($dOdchodu < $kt)) {
                    $malBytVpraci = ($dOdchodu - $dPrichodu) / (24 * 60 * 60) + 1;
                } else if ($dPrichodu > $zt) {
                    $malBytVpraci = ($kt - $dPrichodu) / (24 * 60 * 60) + 1;
                } else if ($dOdchodu < $kt) {
                    $malBytVpraci = ($dOdchodu - $z) / (24 * 60 * 60) + 1;
                } else {
                    $malBytVpraci = 0;
                }
            }
            $fond = $zam['fond'] * 60 * 60;
            $pocetSmien = 0;
            $pocetPn = 0;
            $smena = $fond / 5;
            $nadcas = 0;
            $d_s = "SELECT * FROM dochadzkazamfalk WHERE zid='" . $zam['id'] . "' AND date >= '" . $zt . "' AND date <= '" . $kt . "'";
            $d_q = MySQL_Query($d_s);
            while ($d = MySQL_Fetch_Array($d_q)) {
                if (($d['realOd'] != '') && ($d['realDo'] != '') && (($d['type'] == 'P') || ($d['type'] == 'SPL'))) {
                    $smenaVysledna = $smena + (0.5 * 3600);
                    $pocetSmien++;
                    $odrobene = zistiPocetSekund($d['realOd'], $d['realDo']);
                    $nadcasZaDen = $odrobene - $smenaVysledna;
                    if ($nadcasZaDen < 0) {
                        $n = $nadcasZaDen / 1800;
                        $n = (int) $n;
                        $n = $n / 2;
                        if (($nadcasZaDen % 1800) != 0) {
                            $n = $n - 0.5;
                        }
                        $nadcas += $n;
                    } elseif ($nadcasZaDen > 0) {
                        $n = $nadcasZaDen / 1800;
                        $n = (int) $n;
                        $n = $n / 2;
                        $nadcas += $n;
                    } else {
                        $nadcas += 0;
                    }
                } elseif (($d['type'] == 'D') || ($d['type'] == 'LV') || ($d['type'] == 'LD') || ($d['type'] == 'PN') || ($d['type'] == 'MD') || ($d['type'] == 'OT') || ($d['type'] == 'MOD')) {
                    $pocetSmien++;
                    if ($d['type'] == 'PN')
                        $pocetPn++;
                }
            }
            $pocetMOD = 0;
            if ($zam['manager'] == 1) {
                for ($g = 0; $g < 7; $g++) {
                    $ddd = $zt + ($g * 24 * 60 * 60);
                    $d_s = "SELECT * FROM dochadzkazamfalk WHERE zid='" . $zam['id'] . "' AND date = '" . $ddd . "' AND sviatok!=0 AND (type='DO' OR type='MOD')";
                    $d_q = MySQL_Query($d_s);
                    while ($d = MySQL_Fetch_Array($d_q)) {
                        if ((date("w", $ddd) == 6) || (date("w", $ddd) == 0)) {
                            if ($d['type'] == 'MOD') {
                                //$pocetSmien++;
                            }
                        } else {
                            $pocetSmien++;
                        }
                        //$pocetSmien++;
                    }
                    $d_s = "SELECT * FROM dochadzkazamfalk WHERE zid='" . $zam['id'] . "' AND date = '" . $ddd . "' AND type='MOD'";
                    $d_q = MySQL_Query($d_s);
                    while ($d = MySQL_Fetch_Array($d_q)) {
                        if ($ddd >= 1325721600)
                            $pocetMOD++;
                    }
                }
            }
            ///if ($zam['manager'] != 1) {
                if (($pocetSmien == 6) && ($pocetPn >= 1)) {
                    $pocetSmien = 5;
                } elseif ($pocetSmien == 7) {
                    if ($pocetPn >= 2) {
                        $pocetSmien = 5;
                    } elseif ($pocetPn >= 1) {
                        $pocetSmien = 6;
                    }
                }
            /*} else {
                if ($pocetPn > 5){
                    $rozdiel = $pocetPn - 5;
                    $pocetSmien -= $rozdiel;
                }
            }*/
            if ($full) {
                $chybajuciCas = ((5 - $pocetSmien) * $smena);
                $chybajuciCas = $chybajuciCas / 3600;
                $nadcas = $nadcas - $chybajuciCas;
            } else {
                if ($malBytVpraci > 5)
                    $malBytVpraci = 5;
                $vystup .= $zam['id'] . ': ' . $malBytVpraci . '#';
                $chybajuciCas = (($malBytVpraci - $pocetSmien) * $smena);
                $chybajuciCas = $chybajuciCas / 3600;
                $nadcas = $nadcas - $chybajuciCas;
            }
            if ($pocetMOD != 0) {
                $nadcas = $nadcas - ($pocetMOD * 4);
            }
            $VYSTUP[3] = $nadcas;
            $VYSTUP[4] = $pocetSmien;
            $ul_i = "INSERT INTO vystup (id, zam, date, value, full, smeny, pn) VALUES (NULL, '" . $VYSTUP[1] . "', '" . $VYSTUP[0] . "','" . $VYSTUP[3] . "','" . $VYSTUP[2] . "','" . $VYSTUP[4] . "', '".$pocetPn."')";
            $ul_q = MySQL_Query($ul_i);

            $s_s = "SELECT * FROM falknadcas WHERE zid = '" . $zam['id'] . "' AND date='" . $zt . "'";
            $s_q = MySQL_Query($s_s);
            $s = MySQL_Fetch_Array($s_q);
            if (isset($s['id'])) {
                $uloz_i = "UPDATE falknadcas SET kolko='" . $nadcas . "' WHERE zid='" . $zam['id'] . "' AND date='" . $zt . "'";
                $uloz_q = MySQL_Query($uloz_i);
            } else {
                $uloz_i = "INSERT INTO falknadcas (id, zid, date, mesiac, rok, kolko) VALUES (NULL, '" . $zam['id'] . "', '" . $zt . "','" . date("n", $_REQUEST['date']) . "','" . date("Y", $_REQUEST['date']) . "','" . $nadcas . "')";
                $uloz_q = MySQL_Query($uloz_i);
            }

            vypocitatNocne($zam['id'], $zt);
            vypocitatStravu($zam['id'], $zt);
            vypocitatSviatok($zam['id'], $zt);
        }
    }
    $zt = $zt - (7 * 24 * 60 * 60);
    $kt = $kt - (7 * 24 * 60 * 60);
}
//echo $vystup;
echo 1;
?>
